package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.mq.constants.MqConst;
import com.atguigu.gmall.mq.service.RabbitService;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 支付信息表管理
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RabbitService rabbitService;

    //保存支付信息表
    @Override
    public PaymentInfo savePaymentInfo(Long orderId, PaymentType type) {
        //1:判断支付信息表是否已经保存过了   （提交订单 防止二次提交？ 幂等性问题 解决了吗
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new
                QueryWrapper<PaymentInfo>().eq("order_id", orderId));
        if(null == paymentInfo){
            //2:查询订单信息
            OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
            //3:如果没有保存过 再保存支付信息表   一个用户的订单只能生成一个支付信息
            paymentInfo = new PaymentInfo();
            //对外业务编号
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            //标题
            paymentInfo.setSubject(orderInfo.getTradeBody());
            //订单尖
            paymentInfo.setOrderId(orderId);
            //支付类型
            paymentInfo.setPaymentType(type.name());
            //支付金额
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            //支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            //时间
            paymentInfo.setCreateTime(new Date());
            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }


    //更新支付信息表
    @Override
    public void paySuccess(Map<String, String> paramsMap) {

        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new
                QueryWrapper<PaymentInfo>().
                eq("out_trade_no", paramsMap.get("out_trade_no")));
        //必须是支付中 才可以更新
        if(PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
            //更新为已支付
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //流水号
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //时间
            paymentInfo.setCallbackTime(new Date());
            //将所有Json字符串
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            //更新
            paymentInfoMapper.updateById(paymentInfo);
            //更新订单状态  使用MQ
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());
        }


    }
}
